package com.ruoyi.web.controller.business;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.io.file.FileReader;
import com.ruoyi.business.domain.LocationInfo;
import com.ruoyi.business.domain.NumberManagement;
import com.ruoyi.business.domain.TextReader;
import com.ruoyi.business.mapper.LocationInfoMapper;
import com.ruoyi.business.service.ILocationInfoService;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.reflect.ReflectUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;

import javax.servlet.http.HttpServletResponse;

/**
 * 位置信息Controller
 *
 * @author yangmeng
 * @date 2023-06-09
 */
@Api(tags = "坐标信息")
@RestController
@RequestMapping("/location")
public class LocationInfoController extends BaseController {


    @Autowired
    private ILocationInfoService locationInfoService;

    @Autowired
    private LocationInfoMapper locationInfoMapper;


    /**
     * 查询位置信息列表
     */
    @ApiOperation("查询位置信息列表")
    @GetMapping("/list")
    public AjaxResult list(LocationInfo locationInfo) {
        List<LocationInfo> list = locationInfoService.selectLocationInfoList(locationInfo);
        return success(list);
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response, LocationInfo locationInfo) {
        locationInfo.setBracketLine(1);
        locationInfo.setBracketRow(1);
        List<LocationInfo> list = locationInfoService.selectLocationInfoList(locationInfo);
        List<TextReader> textReaderList = new ArrayList<>();
        for (LocationInfo info : list) {
            String coordinateFive = info.getCoordinateFive();
            TextReader textReader = new TextReader();
            textReader.setName(info.getName());
            String[] split = coordinateFive.split(",");
            textReader.setLongitude(split[0]);
            textReader.setLatitude(split[1]);
            textReaderList.add(textReader);
        }
        ExcelUtil<TextReader> util = new ExcelUtil<>(TextReader.class);
        util.exportExcel(response, textReaderList, "编号坐标信息");
    }

    /**
     * 获取位置信息详细信息
     */
    @ApiOperation("获取位置信息详细信息")
    @GetMapping(value = "/{locationId}")
    public AjaxResult getInfo(@PathVariable("locationId") Long locationId) {
        return success(locationInfoService.selectLocationInfoByLocationId(locationId));
    }

    /**
     * 获取编号管理详细信息
     */
    @GetMapping(value = "/info/{numberId}")
    public AjaxResult info(@PathVariable("numberId") Long numberId) {
        return success(locationInfoService.selectLocationInfoByNumberId(numberId));
    }


    /**
     * 新增位置信息
     */
    @ApiOperation("新增位置信息")
    @PostMapping
    public AjaxResult add(@RequestBody LocationInfo locationInfo) {
        return AjaxResult.success(locationInfoService.insertLocationInfo(locationInfo));
    }


    /**
     * 修改位置信息
     */
    @ApiOperation("修改位置信息")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody LocationInfo locationInfo) {
        return AjaxResult.success(locationInfoService.updateLocationInfo(locationInfo));
    }

    /**
     * 删除位置信息
     */
    @PostMapping("/delete/{locationIds}")
    public AjaxResult remove(@PathVariable Long[] locationIds) {
        return toAjax(locationInfoService.deleteLocationInfoByLocationIds(locationIds));
    }

    @GetMapping("/bb")
    public AjaxResult bb() throws Exception {
        FileReader fileReader = new FileReader("C:\\Users\\admin\\Desktop\\result_40.txt");
        List<String> list = fileReader.readLines();
        List<TextReader> readerList = new ArrayList<>();
        for (String str : list) {
            if (str.contains("Longitude") || str.contains("Latitude") || str.contains("Elevation") || StringUtils.isEmpty(str) || str.contains("please check")) {
                continue;
            }
            String[] filed = StringUtils.splitPreserveAllTokens(str, ",");
            TextReader textReader;
            try {
                textReader = ReflectUtils.getMappingPo(TextReader.class, filed);
                textReader.setCoordinate(textReader.getLongitude() + "," + textReader.getLatitude() + "," + textReader.getElevation());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            readerList.add(textReader);
        }
        // 编号项目ID
        Long numberId = 222L;
        NumberManagement numberManagement = new NumberManagement();
        numberManagement.setNumberId(numberId);
        numberManagement.setAssemblyOrder("3");
        numberManagement.setSeriesOrder("0");
        numberManagement.setBracketRow(2);
        numberManagement.setBracketColumn(10);
        numberManagement.setL1(2);
        numberManagement.setL2(2);
        numberManagement.setR1(12);
        numberManagement.setR2(12);
        numberManagement.setType("1");
        numberManagement.setStationId(35L);
        // 删除项目编号坐标信息
//        locationInfoMapper.deleteLocationInfoByNumberId(numberId);
        Map<String, List<TextReader>> collect = readerList.stream().collect(Collectors.groupingBy(TextReader::getName));
        System.out.println(collect);
        readerList.stream().collect(Collectors.groupingBy(TextReader::getName)).forEach((key, value) -> {
            locationInfoService.numberType1(value, key, numberManagement);
        });
        return AjaxResult.success();
    }

    @GetMapping("/aa")
    public AjaxResult aa() throws Exception {
        FileReader fileReader = new FileReader("C:\\Users\\admin\\Desktop\\八亩佬.txt");
        List<String> list = fileReader.readLines();
        List<TextReader> readerList = new ArrayList<>();
        for (String str : list) {
            if (str.contains("Longitude") || str.contains("Latitude") || str.contains("Elevation") || StringUtils.isEmpty(str)) {
                continue;
            }
            String[] filed = StringUtils.splitPreserveAllTokens(str, ",");
            TextReader textReader;
            try {
                textReader = ReflectUtils.getMappingPo(TextReader.class, filed);
                textReader.setCoordinate(textReader.getLongitude() + "," + textReader.getLatitude() + "," + textReader.getElevation());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            readerList.add(textReader);
        }
        // 编号项目ID
        Long numberId = 82L;
        // 所属电站ID
        Long stationId = 20L;
        // 支架列数
        Integer bracketRow = 26;
        // 支架行数
        Integer bracketColumn = 3;
        // 组件编号顺序
        String assemblyOrder = "0";
        // 组串编号顺序
        String seriesOrder = "0";
        // 删除项目编号坐标信息
        locationInfoMapper.deleteLocationInfoByNumberId(numberId);
        readerList.stream().collect(Collectors.groupingBy(TextReader::getName)).forEach((key, value) -> {



            if (key.contains("please check")) {
                return;
            }
            if (key.contains("@")) {
                String[] arr1 = key.split("@");
                for (String name : arr1) {
                    String[] nameArr = name.split("-");
                    StringBuffer ancestors = new StringBuffer();
                    ancestors.append(0).append(",");
                    String pName = "";
                    Long pid = 0L;
                    for (int i = 0; i < nameArr.length; i++) {
                        LocationInfo locationInfo = new LocationInfo();
                        if (i == nameArr.length -1) {
                            locationInfo.setName(pName + nameArr[i]);
                            locationInfo.setNumberId(numberId);
                            locationInfo.setStationId(stationId);
                            locationInfo.setBracketLine(bracketColumn);
                            locationInfo.setBracketRow(bracketRow);
                            locationInfo.setAncestors(ancestors.toString());
                            locationInfo.setPid(pid);
                            locationInfo.setCoordinateOne(value.get(0).getCoordinate());
                            locationInfo.setCoordinateTwo(value.get(1).getCoordinate());
                            locationInfo.setCoordinateThree(value.get(2).getCoordinate());
                            locationInfo.setCoordinateFour(value.get(3).getCoordinate());
                            locationInfo.setCoordinateFive(add(value.get(0).getLongitude(), value.get(1).getLongitude(), value.get(2).getLongitude(), value.get(3).getLongitude()) + "," + add(value.get(0).getLatitude(), value.get(1).getLatitude(), value.get(2).getLatitude(), value.get(3).getLatitude()) + ",0");
                            locationInfo.setRemark("0");
                            LocationInfo locationInfoDb = locationInfoMapper.checkName(locationInfo);
                            if (locationInfoDb == null) {
                                locationInfoMapper.insertLocationInfo(locationInfo);
                                pid = locationInfo.getLocationId();
                                ancestors.append(locationInfo.getLocationId()).append(",");
                            } else {
                                pid = locationInfoDb.getLocationId();
                                ancestors.append(locationInfoDb.getLocationId()).append(",");
                            }
//                            List<TextReader> sortList = value.stream().sorted(Comparator.comparing(TextReader::getLatitude).thenComparing(TextReader::getLongitude)).collect(Collectors.toList());
                            TextReader[][] array = locationInfoService.calcGPS(bracketColumn, bracketRow, value);
                            if (nameArr[i].contains("~")){
                                String[] numbers = nameArr[i].split("~");
                                // 对纬度进行降序
                                for (int i1 = Convert.toInt(numbers[0]); i1 <= Convert.toInt(numbers[1]); i1++) {
                                    int k = i1 - Convert.toInt(numbers[0]);
                                    LocationInfo locationInfoA = new LocationInfo();
                                    String name_1 = Convert.toStr(i1);
                                    if (i1 < 10) {
                                        name_1 = "0" + i1;
                                    }
                                    locationInfoA.setName(pName + name_1);
                                    locationInfoA.setNumberId(numberId);
                                    locationInfoA.setStationId(stationId);
                                    locationInfoA.setBracketLine(1);
                                    locationInfoA.setBracketRow(bracketRow);
                                    locationInfoA.setAncestors(ancestors.toString());
                                    locationInfoA.setPid(pid);
                                    int index1 = 0;
                                    int index2 = 0;
                                    // 组串编号
                                    if ("0".equals(seriesOrder)) {
                                        index1 = bracketColumn - k;
                                        index2 = bracketColumn - k - 1;
                                    } else if ("0".equals(seriesOrder)) {
                                        index1 = k;
                                        index2 = k + 1;
                                    }
                                    locationInfoA.setCoordinateOne(array[index1][0].getLongitude() + "," + array[index1][0].getLatitude() + "," + array[index1][0].getElevation());
                                    locationInfoA.setCoordinateTwo(array[index2][0].getLongitude() + "," + array[index2][0].getLatitude() + "," + array[index2][0].getElevation());
                                    locationInfoA.setCoordinateThree(array[index1][bracketRow].getLongitude() + "," + array[index1][bracketRow].getLatitude() + "," + array[index1][bracketRow].getElevation());
                                    locationInfoA.setCoordinateFour(array[index2][bracketRow].getLongitude() + "," + array[index2][bracketRow].getLatitude() + "," + array[index2][bracketRow].getElevation());
                                    locationInfoA.setCoordinateFive(add(array[index1][0].getLongitude(), array[index2][0].getLongitude(), array[index1][bracketRow].getLongitude(), array[index2][bracketRow].getLongitude()) + "," +
                                            add(array[index1][0].getLatitude(), array[index2][0].getLatitude(), array[index1][bracketRow].getLatitude(), array[index2][bracketRow].getLatitude()) + ",0");
                                    locationInfoMapper.insertLocationInfo(locationInfoA);
                                    // 组件编号
                                    for (int aLong = 1; aLong <= bracketRow; aLong++) {
                                        String name_0;
                                        if (aLong <10) {
                                            name_0 = "0" + aLong;
                                        } else {
                                            name_0 = "" + aLong;
                                        }
                                        LocationInfo locationInfoB = new LocationInfo();
                                        locationInfoB.setName(locationInfoA.getName() + "-" + name_0);
                                        locationInfoB.setNumberId(numberId);
                                        locationInfoB.setStationId(stationId);
                                        locationInfoB.setBracketLine(1);
                                        locationInfoB.setBracketRow(1);
                                        locationInfoB.setSeriesOrder(seriesOrder);
                                        locationInfoB.setAssemblyOrder(assemblyOrder);
                                        locationInfoB.setAncestors(ancestors.toString() + locationInfoA.getPid());
                                        locationInfoB.setPid(locationInfoA.getLocationId());
                                        // 组件编号顺序：0从左到右，1从右到左
                                        int index3 = 0;
                                        int index4 = 0;
                                        if ("0".equals(assemblyOrder)) {
                                            index3 = aLong -1;
                                            index4 = aLong;
                                        } else if ("1".equals(assemblyOrder)) {
                                            index3 = bracketRow - aLong;
                                            index4 = bracketRow + 1 -aLong;
                                        }
                                        locationInfoB.setCoordinateOne(array[index1][index3].getLongitude() + "," + array[index1][index3].getLatitude() + "," + array[index1][index3].getElevation());
                                        locationInfoB.setCoordinateTwo(array[index1][index4].getLongitude()  + "," + array[index1][index4].getLatitude() + "," + array[index1][index4].getElevation());
                                        locationInfoB.setCoordinateThree(array[index2][index3].getLongitude() + "," + array[index2][index3].getLatitude() + "," + array[index2][index3].getElevation());
                                        locationInfoB.setCoordinateFour(array[index2][index4].getLongitude() + "," + array[index2][index4].getLatitude() + "," + array[index2][index4].getElevation());
                                        locationInfoB.setCoordinateFive(add(array[index1][index3].getLongitude(), array[index2][index3].getLongitude(), array[index1][index4].getLongitude(), array[index2][index4].getLongitude()) + "," +
                                                add(array[index1][index3].getLatitude(), array[index2][index3].getLatitude(), array[index1][index4].getLatitude(), array[index2][index4].getLatitude()) + ",0");
                                        locationInfoMapper.insertLocationInfo(locationInfoB);
                                    }
                                }
                            } else {
                                LocationInfo locationInfoA = new LocationInfo();
                                locationInfoA.setName(pName + nameArr[i]);
                                locationInfoA.setNumberId(numberId);
                                locationInfoA.setStationId(stationId);
                                locationInfoA.setBracketLine(1);
                                locationInfoA.setBracketRow(bracketRow);
                                locationInfoA.setAncestors(ancestors.toString());
                                locationInfoA.setPid(pid);
                                int index1 = 0;
                                int index2 = 0;
                                // 组串编号
                                if ("0".equals(seriesOrder)) {
                                    index1 = 0;
                                    index2 = 1;
                                } else if ("0".equals(seriesOrder)) {
                                    index1 = bracketColumn;
                                    index2 = bracketColumn - 1;
                                }
                                locationInfoA.setCoordinateOne(array[index1][0].getLongitude() + "," + array[index1][0].getLatitude() + "," + array[index1][0].getElevation());
                                locationInfoA.setCoordinateTwo(array[index2][0].getLongitude() + "," + array[index2][0].getLatitude() + "," + array[index2][0].getElevation());
                                locationInfoA.setCoordinateThree(array[index1][bracketRow].getLongitude() + "," + array[index1][bracketRow].getLatitude() + "," + array[index1][bracketRow].getElevation());
                                locationInfoA.setCoordinateFour(array[index2][bracketRow].getLongitude() + "," + array[index2][bracketRow].getLatitude() + "," + array[index2][bracketRow].getElevation());
                                locationInfoA.setCoordinateFive(add(array[index1][0].getLongitude(), array[index2][0].getLongitude(), array[index1][bracketRow].getLongitude(), array[index2][bracketRow].getLongitude()) + "," +
                                        add(array[index1][0].getLatitude(), array[index2][0].getLatitude(), array[index1][bracketRow].getLatitude(), array[index2][bracketRow].getLatitude()) + ",0");
                                locationInfoMapper.insertLocationInfo(locationInfoA);
                                // 组件编号
                                for (int aLong = 1; aLong <= bracketRow; aLong++) {
                                    String name_0;
                                    if (aLong <10) {
                                        name_0 = "0" + aLong;
                                    } else {
                                        name_0 = "" + aLong;
                                    }
                                    LocationInfo locationInfoB = new LocationInfo();
                                    locationInfoB.setName(locationInfoA.getName() + "-" + name_0);
                                    locationInfoB.setNumberId(numberId);
                                    locationInfoB.setStationId(stationId);
                                    locationInfoB.setBracketLine(1);
                                    locationInfoB.setBracketRow(1);
                                    locationInfoB.setSeriesOrder(seriesOrder);
                                    locationInfoB.setAssemblyOrder(assemblyOrder);
                                    locationInfoB.setAncestors(ancestors.toString() + locationInfoA.getPid());
                                    locationInfoB.setPid(locationInfoA.getLocationId());
                                    // 组件编号顺序：0从左到右，1从右到左
                                    int index3 = 0;
                                    int index4 = 0;
                                    if ("0".equals(assemblyOrder)) {
                                        index3 = aLong -1;
                                        index4 = aLong;
                                    } else if ("1".equals(assemblyOrder)) {
                                        index3 = bracketRow - aLong;
                                        index4 = bracketRow + 1 -aLong;
                                    }
                                    locationInfoB.setCoordinateOne(array[index1][index3].getLongitude() + "," + array[index1][index3].getLatitude() + "," + array[index1][index3].getElevation());
                                    locationInfoB.setCoordinateTwo(array[index1][index4].getLongitude()  + "," + array[index1][index4].getLatitude() + "," + array[index1][index4].getElevation());
                                    locationInfoB.setCoordinateThree(array[index2][index3].getLongitude() + "," + array[index2][index3].getLatitude() + "," + array[index2][index3].getElevation());
                                    locationInfoB.setCoordinateFour(array[index2][index4].getLongitude() + "," + array[index2][index4].getLatitude() + "," + array[index2][index4].getElevation());
                                    locationInfoB.setCoordinateFive(add(array[index1][index3].getLongitude(), array[index2][index3].getLongitude(), array[index1][index4].getLongitude(), array[index2][index4].getLongitude()) + "," +
                                            add(array[index1][index3].getLatitude(), array[index2][index3].getLatitude(), array[index1][index4].getLatitude(), array[index2][index4].getLatitude()) + ",0");
                                    locationInfoMapper.insertLocationInfo(locationInfoB);
                                }
                            }
                        } else{
                            locationInfo.setName(pName + nameArr[i]);
                            locationInfo.setNumberId(numberId);
                            locationInfo.setStationId(stationId);
                            locationInfo.setBracketLine(bracketColumn);
                            locationInfo.setBracketRow(bracketRow);
                            locationInfo.setAncestors(ancestors.toString());
                            locationInfo.setPid(pid);
                            LocationInfo locationInfoDb = locationInfoMapper.checkName(locationInfo);
                            if (locationInfoDb == null) {
                                locationInfoMapper.insertLocationInfo(locationInfo);
                                pid = locationInfo.getLocationId();
                                ancestors.append(locationInfo.getLocationId()).append(",");
                                pName = locationInfo.getName() + "-";
                            } else {
                                pid = locationInfoDb.getLocationId();
                                ancestors.append(locationInfoDb.getLocationId()).append(",");
                                pName = locationInfoDb.getName() + "-";
                            }
                        }
                    }
                }
            } else {
                String[] nameArr = key.split("-");
                StringBuffer ancestors = new StringBuffer();
                ancestors.append(0).append(",");
                String pName = "";
                Long pid = 0L;
                for (int i = 0; i < nameArr.length; i++) {
                    LocationInfo locationInfo = new LocationInfo();
                    if (i == nameArr.length -1) {
                        locationInfo.setName(pName + nameArr[i]);
                        locationInfo.setNumberId(numberId);
                        locationInfo.setStationId(stationId);
                        locationInfo.setBracketLine(bracketColumn);
                        locationInfo.setBracketRow(bracketRow);
                        locationInfo.setAncestors(ancestors.toString());
                        locationInfo.setPid(pid);
                        locationInfo.setCoordinateOne(value.get(0).getCoordinate());
                        locationInfo.setCoordinateTwo(value.get(1).getCoordinate());
                        locationInfo.setCoordinateThree(value.get(2).getCoordinate());
                        locationInfo.setCoordinateFour(value.get(3).getCoordinate());
                        locationInfo.setCoordinateFive(add(value.get(0).getLongitude(), value.get(1).getLongitude(), value.get(2).getLongitude(), value.get(3).getLongitude()) + "," + add(value.get(0).getLatitude(), value.get(1).getLatitude(), value.get(2).getLatitude(), value.get(3).getLatitude()) + ",0");
                        locationInfo.setRemark("0");
                        LocationInfo locationInfoDb = locationInfoMapper.checkName(locationInfo);
                        if (locationInfoDb == null) {
                            locationInfoMapper.insertLocationInfo(locationInfo);
                            pid = locationInfo.getLocationId();
                            ancestors.append(locationInfo.getLocationId()).append(",");
                        } else {
                            pid = locationInfoDb.getLocationId();
                            ancestors.append(locationInfoDb.getLocationId()).append(",");
                        }
                        if (nameArr[i].contains("~")){
//                            List<TextReader> sortList = value.stream().sorted(Comparator.comparing(TextReader::getLatitude).thenComparing(TextReader::getLongitude)).collect(Collectors.toList());
                            TextReader[][] array = locationInfoService.calcGPS(bracketColumn, bracketRow, value);
                            String[] numbers = nameArr[i].split("~");
                            // 对纬度进行降序
                            for (int i1 = Convert.toInt(numbers[0]); i1 <= Convert.toInt(numbers[1]); i1++) {
                                int k = i1 - Convert.toInt(numbers[0]);
                                LocationInfo locationInfoA = new LocationInfo();
                                String name_1 = Convert.toStr(i1);
                                if (i1 < 10) {
                                    name_1 = "0" + i1;
                                }
                                locationInfoA.setName(pName + name_1);
                                locationInfoA.setNumberId(numberId);
                                locationInfoA.setStationId(stationId);
                                locationInfoA.setBracketLine(1);
                                locationInfoA.setBracketRow(bracketRow);
                                locationInfoA.setAncestors(ancestors.toString());
                                locationInfoA.setPid(pid);
                                int index1 = 0;
                                int index2 = 0;
                                // 组串编号
                                if ("0".equals(seriesOrder)) {
                                    index1 = bracketColumn - k;
                                    index2 = bracketColumn - k - 1;
                                } else if ("0".equals(seriesOrder)) {
                                    index1 = k;
                                    index2 = k + 1;
                                }
                                locationInfoA.setCoordinateOne(array[index1][0].getLongitude() + "," + array[index1][0].getLatitude() + "," + array[index1][0].getElevation());
                                locationInfoA.setCoordinateTwo(array[index2][0].getLongitude() + "," + array[index2][0].getLatitude() + "," + array[index2][0].getElevation());
                                locationInfoA.setCoordinateThree(array[index1][bracketRow].getLongitude() + "," + array[index1][bracketRow].getLatitude() + "," + array[index1][bracketRow].getElevation());
                                locationInfoA.setCoordinateFour(array[index2][bracketRow].getLongitude() + "," + array[index2][bracketRow].getLatitude() + "," + array[index2][bracketRow].getElevation());
                                locationInfoA.setCoordinateFive(add(array[index1][0].getLongitude(), array[index2][0].getLongitude(), array[index1][bracketRow].getLongitude(), array[index2][bracketRow].getLongitude()) + "," +
                                        add(array[index1][0].getLatitude(), array[index2][0].getLatitude(), array[index1][bracketRow].getLatitude(), array[index2][bracketRow].getLatitude()) + ",0");
                                locationInfoMapper.insertLocationInfo(locationInfoA);
                                // 组件编号
                                for (int aLong = 1; aLong <= bracketRow; aLong++) {
                                    String name_0;
                                    if (aLong <10) {
                                        name_0 = "0" + aLong;
                                    } else {
                                        name_0 = "" + aLong;
                                    }
                                    LocationInfo locationInfoB = new LocationInfo();
                                    locationInfoB.setName(locationInfoA.getName() + "-" + name_0);
                                    locationInfoB.setNumberId(numberId);
                                    locationInfoB.setStationId(stationId);
                                    locationInfoB.setBracketLine(1);
                                    locationInfoB.setBracketRow(1);
                                    locationInfoB.setSeriesOrder(seriesOrder);
                                    locationInfoB.setAssemblyOrder(assemblyOrder);
                                    locationInfoB.setAncestors(ancestors.toString() + locationInfoA.getPid());
                                    locationInfoB.setPid(locationInfoA.getLocationId());
                                    // 组件编号顺序：0从左到右，1从右到左
                                    int index3 = 0;
                                    int index4 = 0;
                                    if ("0".equals(assemblyOrder)) {
                                        index3 = aLong -1;
                                        index4 = aLong;
                                    } else if ("1".equals(assemblyOrder)) {
                                        index3 = bracketRow - aLong;
                                        index4 = bracketRow + 1 -aLong;
                                    }
                                    locationInfoB.setCoordinateOne(array[index1][index3].getLongitude() + "," + array[index1][index3].getLatitude() + "," + array[index1][index3].getElevation());
                                    locationInfoB.setCoordinateTwo(array[index1][index4].getLongitude()  + "," + array[index1][index4].getLatitude() + "," + array[index1][index4].getElevation());
                                    locationInfoB.setCoordinateThree(array[index2][index3].getLongitude() + "," + array[index2][index3].getLatitude() + "," + array[index2][index3].getElevation());
                                    locationInfoB.setCoordinateFour(array[index2][index4].getLongitude() + "," + array[index2][index4].getLatitude() + "," + array[index2][index4].getElevation());
                                    locationInfoB.setCoordinateFive(add(array[index1][index3].getLongitude(), array[index2][index3].getLongitude(), array[index1][index4].getLongitude(), array[index2][index4].getLongitude()) + "," +
                                            add(array[index1][index3].getLatitude(), array[index2][index3].getLatitude(), array[index1][index4].getLatitude(), array[index2][index4].getLatitude()) + ",0");
                                    locationInfoMapper.insertLocationInfo(locationInfoB);
                                }
                            }

                        }
                    } else{
                        locationInfo.setName(pName + nameArr[i]);
                        locationInfo.setNumberId(numberId);
                        locationInfo.setStationId(stationId);
                        locationInfo.setBracketLine(bracketColumn);
                        locationInfo.setBracketRow(bracketRow);
                        locationInfo.setAncestors(ancestors.toString());
                        locationInfo.setPid(pid);
                        LocationInfo locationInfoDb = locationInfoMapper.checkName(locationInfo);
                        if (locationInfoDb == null) {
                            locationInfoMapper.insertLocationInfo(locationInfo);
                            pid = locationInfo.getLocationId();
                            ancestors.append(locationInfo.getLocationId()).append(",");
                            pName = locationInfo.getName() + "-";
                        } else {
                            pid = locationInfoDb.getLocationId();
                            ancestors.append(locationInfoDb.getLocationId()).append(",");
                            pName = locationInfoDb.getName() + "-";
                        }
                    }
                }
            }
        });
        return AjaxResult.success();
    }

    public String add(String str1, String str2, String str3, String str4) {
        BigDecimal d1 = new BigDecimal(str1);
        BigDecimal d2 = new BigDecimal(str2);
        BigDecimal d3 = new BigDecimal(str3);
        BigDecimal d4 = new BigDecimal(str4);
        BigDecimal d0 = new BigDecimal(4);
        BigDecimal bigDecimal = d1.add(d2).add(d3).add(d4).divide(d0, 20, BigDecimal.ROUND_HALF_UP);
        return bigDecimal.toString();
    }
}
